package org.example;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDAO {
    public List<Order> getAllOrders() {
        List<Order> orders = new ArrayList<>();
        String sql = "SELECT * FROM orders"; // 查询所有订单
        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                Order order = new Order();
                order.setId(rs.getInt("id"));
                order.setBookId(rs.getString("book_id")); // 获取书籍ID
                order.setBookName(rs.getString("book_name"));
                order.setBookPrice(rs.getDouble("book_price")); // 获取书籍价格
                order.setPhone(rs.getString("phone"));
                order.setAddress(rs.getString("address"));
                order.setBuyerName(rs.getString("buyer_name"));
                orders.add(order);
            }
        } catch (SQLException e) {
            e.printStackTrace(); // 打印异常信息
        }
        return orders;
    }
}

